/*
 * IconButton.fx
 *
 * Created on Apr 12, 2010, 3:00:25 PM
 */

package fxgreader;
import javafx.scene.image.ImageView;
import javafx.scene.shape.Rectangle;
import javafx.scene.image.Image;
import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.scene.input.MouseEvent;
import javafx.scene.Cursor;

/**
 * @author perkintang
 */
public def STAR : Integer = 0;
public def LIKE : Integer = 64;
public def SHARE : Integer = 32;
public class IconButton extends CustomNode{
var nextOperation = Main.api.ADD;
public var marked : Boolean = false on replace oldvalue{
    if(marked){
        x = 0;
        nextOperation = Main.api.REMOVE;
    }else{
        x = 64;
        nextOperation = Main.api.ADD;
    }
};
public var image : Image = Resources.buttonImage;
public function onclick():Void{
     if(y == STAR){
          Main.api.onMarkSuccess = function(article : Article){
                  article.setStarred();
                  clickReturn(true);
          };
          Main.api.markArticle(Main.currentArticle, ReaderAPI.STAR,nextOperation,true);
     }
     if(y == SHARE){
          Main.api.onMarkSuccess = function(article : Article){
                  article.setShared();
                  clickReturn(true);
          };
          Main.api.markArticle(Main.currentArticle, ReaderAPI.SHARE,nextOperation,true);
     }
     if(y == LIKE){
          Main.api.onMarkSuccess = function(article : Article){
                  article.setLiked();
                  clickReturn(true);
          };
          Main.api.markArticle(Main.currentArticle, ReaderAPI.LIKE,nextOperation,true);
     }
};
public var y : Integer = 0;
var x : Integer = 64;
def cy = Rectangle {
    translateX:bind x
    translateY:bind y
    x: 0  y: 0
    width: 32  height: 32
};
def button : ImageView= ImageView {
        translateX : bind -cy.translateX
        translateY : bind -cy.translateY
        image: bind image
        clip : cy
        cache : true
        onMouseEntered:function(event:MouseEvent):Void{
            if(not marked){
                x = 32;
            }
//            starButton.cursor = Cursor.HAND;
        }
        onMouseExited:function(event:MouseEvent):Void{
            if(not marked){
                x = 64;
            }
//            starButton.cursor = Cursor.DEFAULT;
        }
        onMouseClicked:function(event:MouseEvent):Void{
            if(button.cursor == Cursor.WAIT){
                    return;
            }
//            if(marked){
//                x = 32;
//            }else{
//                x = 0;
//            }
            button.cursor = Cursor.WAIT;
            FX.deferAction(onclick);
        }
};
public function clickReturn(success : Boolean){
        button.cursor = Cursor.HAND;
        if(success){
             marked = not marked;
        }else{
            //TODO : show user some message
        }
}
override protected function create () : Node {
    button.blocksMouse = true;
    button.cursor = Cursor.HAND;
    button;
}
}
